package com.wsc.wsckeeper.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.base.BaseActivity;
import com.wsc.wsckeeper.listeners.TextWatcherImpl;
import com.wsc.wsckeeper.managers.FileServiceManager;
import com.wsc.wsckeeper.managers.PreferenceManager;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.managers.ScreenManager;

public class SettingsActivity extends BaseActivity {
	private List<EditText> components = new ArrayList<EditText>();
	private static final int REQUEST_CODE = 1001;
	private PreferenceManager preference = PreferenceManager.getInstance();

	private TextView tvHide;
	private CheckBox cbRequiredPwd;
	private EditText etPasswordOld;
	private EditText etPasswordNew;
	private EditText etPasswordConfirm;
	private EditText etLocation;
	private ImageView ivChooseLocation;
	private Button btAction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_settings);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initComponents() {
		cbRequiredPwd = (CheckBox) findViewById(R.id.requiredPwd);
		tvHide = (TextView) findViewById(R.id.hide);

		etPasswordOld = (EditText) findViewById(R.id.passwordOld);
		etPasswordNew = (EditText) findViewById(R.id.passwordNew);
		etPasswordConfirm = (EditText) findViewById(R.id.passwordConfirm);

		etLocation = (EditText) findViewById(R.id.location);
		ivChooseLocation = (ImageView) findViewById(R.id.chooseLocation);

		btAction = (Button) findViewById(R.id.action);
	}

	@Override
	public void initValues() {
		components.add(etPasswordOld);
		components.add(etPasswordNew);
		components.add(etPasswordConfirm);

		getActionBar().setSubtitle(R.string.action_settings);
		cbRequiredPwd.setChecked(preference.getNeedLogin());

		etLocation.setText(preference.getLocation());
		btAction.setEnabled(false);
	}

	@Override
	public void initListeners() {
		for (int i = 0; i < components.size(); i++) {
			final EditText edit = components.get(i);
			edit.addTextChangedListener(new TextWatcherImpl() {

				@Override
				public void afterTextChanged(Editable s) {
					btAction.setEnabled(true);
					edit.setError(null);
				}
				
			});
		}

		tvHide.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				etPasswordOld.setHint(preference.getPassword());
				return false;
			}
		});

		cbRequiredPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				etPasswordOld.setHint(ResourceManager.getStringValue(R.string.settings_password_old));
				etPasswordOld.setEnabled(isChecked);
				etPasswordNew.setEnabled(isChecked);
				etPasswordConfirm.setEnabled(isChecked);

				if (!isChecked) {
					etPasswordOld.setText("");
					etPasswordNew.setText("");
					etPasswordConfirm.setText("");
					etPasswordOld.setError(null);
					etPasswordNew.setError(null);
					etPasswordConfirm.setError(null);
				}
			}
		});

		etPasswordOld.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				etPasswordOld.setError(null);
				etPasswordOld.setHint(ResourceManager.getStringValue(R.string.settings_password_old));
			}
		});

		etPasswordConfirm.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				etPasswordConfirm.setError(null);
			}
		});

		btAction.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isValidFields()) {
					preference.setNeedLogin(cbRequiredPwd.isChecked());
					if (cbRequiredPwd.isChecked()) {
						preference.setPassword(etPasswordNew.getText().toString());
						etPasswordOld.setText("");
						etPasswordNew.setText("");
						etPasswordConfirm.setText("");
					}

					preference.setLocation(etLocation.getText().toString());
					FileServiceManager.getInstance().onRefresh();

					finish();
				}
			}
		});

		ivChooseLocation.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this, ChooseFileActivity.class);
				intent.putExtra(ChooseFileActivity.CURRECT_FILE, etLocation.getText().toString());
				ScreenManager.getInstance().forwardForResult(SettingsActivity.this, intent, REQUEST_CODE);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			etLocation.setText(data.getExtras().getString(ChooseFileActivity.CURRECT_FILE));
			btAction.setEnabled(true);
		}
	}

	private boolean isValidFields() {
		boolean isValid = true;
		if (!etPasswordOld.getText().toString().equalsIgnoreCase(preference.getPassword())) {
			etPasswordOld.setError(ResourceManager.getStringValue(R.string.alert_password_old));
			isValid = false;
		}
		if (!etPasswordNew.getText().toString().equalsIgnoreCase(etPasswordConfirm.getText().toString())) {
			etPasswordConfirm.setError(ResourceManager.getStringValue(R.string.alert_password_confirm));
			isValid = false;
		}

		return isValid;
	}
}
